/**
 * @description 工具栏：查询组件
 * @author shenhong
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { FirstHeaderC, TableC } from '../../../../../comps';

@Form.create()
class SamePublishe extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      bondID: '',
      dataSource: undefined,
      columns: undefined,
      pageInfo: {},
    };
  }

  componentDidMount() {
    const { dispatch, bondID } = this.props;
    //获取发行人列表
    dispatch({
      type: 'basicEventM/findtableDta',
      payload: {
        bondID: bondID,
        currentPage: 1,
        numInPage: 20,
      },
      callback: (res) => {
        this.setState({
          dataSource: res.data.dataSource,
          columns: res.data.columns,
          pageInfo: res.data.pageInfo,
        });
      },
    });
  }

  //bondId改变时执行的函数
  getNewData = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'basicEventM/findtableDta',
      payload: {
        bondID: this.state.bondID,
        currentPage: 1,
        numInPage: 20,
      },
      callback: (res) => {
        this.setState({
          dataSource: res.data.dataSource,
          columns: res.data.columns,
          pageInfo: res.data.pageInfo,
        });
      },
    });
  };

  UNSAFE_componentWillReceiveProps(nextProps) {
    //判断是否是最新的bondID
    if (nextProps.bondID != this.state.bondID) {
      this.setState(
        {
          bondID: nextProps.bondID,
        },
        () => {
          this.props.form.resetFields();
          this.getNewData();
        },
      );
    }
  }

  //页码改变事件
  onTableChange = (current, size) => {
    const { dispatch } = this.props;
    this.setState({
      columns: undefined,
      dataSource: undefined,
    });
    dispatch({
      type: 'basicEventM/findtableDta',
      payload: {
        bondID: this.state.bondID,
        currentPage: current,
        numInPage: size,
      },
      callback: (res) => {
        this.setState({
          dataSource: res.data.dataSource,
          columns: res.data.columns,
          pageInfo: res.data.pageInfo,
        });
      },
    });
  };

  //行权表格
  setColumns = (columns) => {
    const arr = columns;
    if (arr && arr.length) {
      arr.map((item) => {
        if (item.title == '剩余期限' || item.title == '票面利率' || item.title == '估值收益率') {
          item.type = 'number';
        } else if (item.title == '估值净价') {
          item.type = 'money';
        }
      });
    }

    return arr;
  };

  render() {
    const { pageInfo, dataSource, columns } = this.state;
    const newColumns = this.setColumns(columns);
    return (
      <div style={{ marginTop: '20px' }}>
        <FirstHeaderC title='同一发行人债券信息' />
        <div>
          <TableC
            dataSource={dataSource}
            columns={newColumns}
            defaultCurrent={pageInfo.currentPage}
            total={pageInfo.total}
            current={pageInfo.currentPage}
            pageSize={pageInfo.numInPage}
            onShowSizeChange={(current, size) => this.onTableChange(current, size)}
            onPageChange={(page, pageSize) => this.onTableChange(page, pageSize)}
          />
        </div>
      </div>
    );
  }
}

export default connect(({ basicEventM }) => ({ basicEventM }))(SamePublishe);
